
//
//  CustomerNetworking.m
//  UI17_AFNetworking封装
//
//  Created by dllo on 15/9/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CustomerNetworking.h"
#import "AFNetworking.h"

@implementation CustomerNetworking

+(void)customerWithURL:(NSString *)urlStirng andParamer:(NSDictionary *)parameters andBlock:(ConnectionBlock)block {
    CustomerNetworking *customer = [[CustomerNetworking alloc]init];
    [customer connectWithURL:urlStirng andParamer:parameters andBlock:block];
    
}



-(void)connectWithURL:(NSString *)urlString andParamer:(NSDictionary *)parameters andBlock:(ConnectionBlock)block {
    
    //开始网络请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    //通过判断parameters是否为空, 确定使用POST or GET
    //如果parameters为空,选择GET
    //如果parameters不为空,选择POST
    if (parameters == nil) {
        //GET请求
        [manager GET:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            block(responseObject);
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"错误信息%@", error);
//            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有您要搜索的东西~"  preferredStyle:UIAlertControllerStyleAlert];
//            
//            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//            
//            UIAlertAction *defautAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
//            [alert addAction:cancelAction];
//            [alert addAction:defautAction];
//            
//            [self presentViewController:alert animated:YES completion:nil];
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络错误" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];

            
            
        }];
    }else{
        //POST请求
        [manager POST:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            block(responseObject);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"错误信息%@", error);
        }];
      
    }
    
   
    
    
}





@end
